<?php

/**
 * The SearchResultCore class is a controller of the Design Pattern.
 * 
 * @abstract LayoutPage
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
class SearchResultCore extends LayoutPage {

    /** @var int The total to be listed in the pagination */
    protected static $_pgtotal = 6;

    /** @var array The research data of vehicles */
    public static $data;

    /** @var stdClass The menu pagination */
    public static $pagination;

    /** @var array The search parameters */
    public static $params = array();

    /** @var string The url of the research */
    public static $url = 'pesquisa/';

    /** @var int The page number */
    public static $page = 1;

    public function preProcess() {
        parent::preProcess();
        foreach (explode('/', $_GET['params']) as $param) {
            if (empty($param))
                break;
            $vector = explode('.', $param);
            if ($vector[0] == 'pagina')
                self::$page = $vector[1];
            else {
                $value = $vector[1] = substr($param, strlen($vector[0]) + 1);
                switch ($vector[0]) {
                    case 'tipo':
                        $field = 'type';
                        if ($type = DbGet::types($value))
                            $value = $type->id;
                        else
                            $value = null;
                        break;
                    case 'marca':
                        $field = 'brand';
                        if ($brand = DbGet::brands(self::$params['type'], $value))
                            $value = $brand->id;
                        else
                            $value = null;
                        break;
                    case 'modelo':
                        $field = 'model';
                        if ($model = DbGet::models(self::$params['type'], self::$params['brand'], $value))
                            $value = $model->id;
                        else
                            $value = null;
                        break;
                    case 'versao':
                        $field = 'version';
                        if ($version = DbGet::versions($value)) {
                            $value = $version->id;
                        } else {
                            $value = null;
                        }
                        break;
                    case 'ano-inicial':
                        $field = 'initialyear';
                        if (!is_numeric($value))
                            $value = null;
                        break;
                    case 'ano-final':
                        $field = 'finalyear';
                        if (!is_numeric($value))
                            $value = null;
                        break;
                    case 'preco-inicial':
                        $field = 'initialprice';
                        if (!is_numeric($value))
                            $value = null;
                        break;
                    case 'preco-final':
                        $field = 'finalprice';
                        if (!is_numeric($value))
                            $value = null;
                        break;
                    case 'anunciante':
                        $field = 'advertiser';
                        if ($value == 'particular')
                            $value = 'customer';
                        elseif ($value == 'revenda')
                            $value = 'enterprise';
                        else
                            $value = null;
                        break;
                    case 'cidade':
                        $field = 'city';
                        if ($city = DbGet::city(str_replace("-", " ", $value)))
                            $value = $city->id;
                        else
                            $value = null;
                        break;
                    default:
                        $value = null;
                        break;
                }

                if ($value != null) {
                    self::$url .= $vector[0] . '.' . urlencode($vector[1]) . '/';
                    self::$params = array_merge(self::$params, array($field => $value));
                }
            }
        }

        if (!isset(self::$params['type']) && !isset(self::$params['advertiser'])) {
            header('Location: ' . __BASE_URI__);
            exit();
        }

        self::$pagination = Util::pagination(self::$page, self::$_pgtotal, DbGet::vehicles_numrows(self::$params), self::$url);

        if (self::$pagination)
            self::$page = self::$pagination->pg;
        self::$data = DbGet::vehicles_search(self::$page, self::$_pgtotal, self::$params);
    }

    public function displayHeader() {
        global $_tag_title, $_metatag_description, $_metatag_keywords;

        $_tag_title = 'Resultado da Busca | Goiautos.com.br';
        $_metatag_description = 'Ofertas de veículos usados, seminovos e novos na nossa região.';
        $_metatag_keywords = str_word_count(mb_strtolower('carros motos novos seminovos usados caminhões veículos classificados goiás busca resultado', 'UTF-8'), 1, 'áéíóúàâêôçãõ0123456789');

        parent::displayHeader();
    }

    public function process() {
        parent::process();

        if (count(self::$data) > 3)
            self::$banners['search'] = Util::view_banners('search');
        self::$banners['lateral2'] = Util::view_banners('lateral2');
    }

    public function displayContent() {
        parent::displayContent();

        Shell::addWidget(array('new-search', 'search-sought', 'search-refine'), 'sidebar-left');
        Shell::showPage('search-result');
    }

}